package main

import (
	"log"
	"time"
	"net/http"
	"io/ioutil"
	"sync"
	"fmt"

	"github.com/tcnksm/go-httpstat"
)

var (
	httpClient = http.Client{Timeout: time.Duration(1 * time.Minute)}
	logger *log.Logger
)

func fetchUrl(url string, wg *sync.WaitGroup) {
	defer wg.Done()

	resp, err := http.Get(url)
	if err != nil {
		fmt.Printf("url=%s, err=%v\n", url, err)
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("url=%s, err=%v\n", url, err)
		return
	}
	err = client.Set(url, body, 0).Err()
	if err != nil {
		fmt.Printf("redis set failed!url=%s\n", url)
	}
	fmt.Printf("fetch url success!url=%s\n", url)
}

func httpGet(url string) []byte{
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Printf("%s\n", err)
	}

	var result httpstat.Result
	ctx := httpstat.WithHTTPStat(req.Context(), &result)
	req = req.WithContext(ctx)

	_client := http.DefaultClient


	resp, err := _client.Do(req)
	if err != nil {
		fmt.Printf("url=%s, err=%v\n", url, err)
		return nil
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("url=%s, err=%v\n", url, err)
	}

	result.End(time.Now())

	logger.Printf("\n%s\n%+v", url, result)

	return body
}
